{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "import urllib.request\n",
    "import requests\n",
    "import re\n",
    "import ast"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Server': 'JSP3/2.0.14', 'Date': 'Sat, 13 Mar 2021 10:20:49 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': '0', 'Connection': 'keep-alive', 'Location': '/Account/Login?ReturnUrl=%2fDownload%2fGet%2f103787', 'Set-Cookie': 'ARRAffinity=09a0fb7f09a024385a56a0797552ad6d626c8c7a6d0995217383760392d238f9;Path=/;HttpOnly;Domain=officeplus.chinacloudsites.cn', 'Accept-Ranges': 'bytes', 'Cache-Control': 'private', 'X-AspNetMvc-Version': '4.0', 'X-AspNet-Version': '4.0.30319', 'X-Powered-By': 'ASP.NET', 'Timing-Allow-Origin': '*', 'Ohc-File-Size': '0'}"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = requests.Session()\n",
    "raw_url = 'http://www.officeplus.cn/Download/Get/103787'\n",
    "\n",
    "headers_str = '''\n",
    "Host: www.officeplus.cn\n",
    "Connection: keep-alive\n",
    "Upgrade-Insecure-Requests: 1\n",
    "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36\n",
    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\n",
    "Referer: http://www.officeplus.cn/p/87/103787.shtml\n",
    "Accept-Encoding: gzip, deflate\n",
    "Accept-Language: zh-CN,zh;q=0.9\n",
    "Cookie: manto_vjid=3cb73e5184fee2e9bc75c594bb162449; t_id=c3171b48-10ed-4b4c-b8ec-83c63cf235f0; t_ctime=1615601431469; loginStatus=active; lo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azHuXS2xXA11ULUz6_ZQB5eOz7kZH9n9c_BwKy33Nhdolidf3pEMGZNbpB7x67OF-mhqOYNB3htsffosjNc64cVFo4Kid7kegCw4ghzntTN9XfQNJMd1DzY3XE-GSiNpcPKbuGRH8MLTaL3gwLPjKYVpGOOeTe3-pOfqUUC62xa0kC-RsRh_tkMOwnU35UItkTC2BR1Uapuo1LdbGGHcOH4MbtpykXT65xaNnbMDUSUeVHo4T2PRHPwrQ; t_oid=ox05quD_5w21HOCX1dkiANT1lROw; Hm_lvt_c1b8f67dbb5e69873155161621f66842=1615601432,1615625207; Hm_lpvt_c1b8f67dbb5e69873155161621f66842=1615626402\n",
    "'''\n",
    "\n",
    "headers_re = re.sub('(.*?): (.*?)\\n',r\"'\\1':'\\2',\\n\",headers_str)\n",
    "headers_re = '{' + headers_re.replace('\\n','') +'}'\n",
    "headers_re = ast.literal_eval(headers_re) # 字符串转字典\n",
    "\n",
    "# res = s.head(raw_url,headers=headers_re)\n",
    "res = s.get(raw_url,headers=headers_re,allow_redirects=False)\n",
    "url = res.headers['Location']\n",
    "res.headers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
